package com.ktjiaoyu.crm.web.controller;
import com.ktjiaoyu.crm.entity.Chance;
import com.ktjiaoyu.crm.entity.Plan;
import com.ktjiaoyu.crm.entity.User;
import com.ktjiaoyu.crm.service.impl.ChanceServiceImpl;
import com.ktjiaoyu.crm.service.impl.PlanServiceImpl;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 开发计划控制器
 * @author Administrator
 */

@Controller
public class PlanController {
    @Resource
    private PlanServiceImpl planService;

    @Resource
    private ChanceServiceImpl chanceService;

    @RequestMapping(value = "/plan/list")
    public String List(String chcCustName, String chcTitle, String chcLinkman,
                       Model model, HttpSession session,
                       @RequestParam(required = false, defaultValue = "1") int pageIndex){
        Sort sort = new Sort(Sort.Direction.DESC, "chcDueDate");
        Pageable pageable = PageRequest.of(pageIndex-1, 5, sort);
        User loginUser = (User) session.getAttribute("loginUser");
        Page<Chance> chancesPage = null;

        Subject subject = SecurityUtils.getSubject();
        if (subject.hasRole("客户经理")){
            chancesPage = chanceService.findChanceByChcDueId(chcCustName, chcTitle, chcLinkman, loginUser.getUsrId(), pageable);
        }else {
            chancesPage = chanceService.findAllChance(chcCustName, chcTitle, chcLinkman, null, null, null, pageable);
        }
        //判断当前用户是否是客户经理
        model.addAttribute("chcCustName", chcCustName);
        model.addAttribute("chcLinkman", chcLinkman);
        model.addAttribute("chcTitle", chcTitle);
        model.addAttribute("chancesPage", chancesPage);
        return "plan/list";
    }

    @RequestMapping(value = "/plan/formulate")
    public String formulate(Long chcId, Model model){
        Chance chance = chanceService.getChance(chcId);
        List<Plan> plans = planService.findPlanByChcId(chcId);
        String plaDate = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        model.addAttribute("chance", chance);
        model.addAttribute("plans", plans);
        model.addAttribute("plaDate", plaDate);
        return "plan/formulate";
    }

    @RequestMapping(value = "/plan/savePlan")
    public String addPlan(Long chcId, String plaDate, String plaTodo, Model model) throws ParseException {
        //获得该销售机会信息
        Chance chance = chanceService.getChance(chcId);
        //新建一个plan实体
        Plan plan = new Plan();
        //添加信息
        plan.setChance(chance);
        plan.setPlaDate(new SimpleDateFormat("yyyy-MM-dd").parse(plaDate));
        plan.setPlaTodo(plaTodo);
        planService.save(plan);
        return "redirect:/plan/list";
    }

    @RequestMapping(value = "/plan/del")
    @ResponseBody
    public Map del(Long plaId){
        Map map = new HashMap();
        try {
            planService.delPlan(plaId);
            map.put("delResult", "true");
        }catch (Exception e){
            map.put("delResult", "false");
        }
        return map;
    }

    @RequestMapping(value = "/plan/editPlaTodo")
    public String editPlaTodo(Long plaId, Model model){
         Plan plan = planService.findPlanByPlaId(plaId);
         model.addAttribute("plan", plan);
        return "plan/editplatodo";
    }

    @RequestMapping(value = "/plan/plaTodoSave")
    public String plaTodoSave(String plaTodo, Long plaId){
        this.planService.updatePlaTodoById(plaTodo, plaId);
        return "redirect:/plan/list";
    }

    @RequestMapping(value = "/plan/execute")
    public String execute(Long chcId, Model model){
        Chance chance = chanceService.getChance(chcId);
        List<Plan> plans = planService.findPlanByChcId(chcId);
        model.addAttribute("chance", chance);
        model.addAttribute("plans", plans);
        return "plan/execute";
    }

    @RequestMapping(value = "/plan/executesave")
    @ResponseBody
    public Map executeSave(String plaResult, Long plaId) {
        Map map = new HashMap();
        try {
            this.planService.updatePlaResultByPlaId(plaResult,plaId);
            map.put("result", "true");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("result", "false");
        }
        return map;
    }

    @RequestMapping(value = "/plan/editStatus")
    @ResponseBody
    public Map editStatus(String chcStatus, Long chcId){
        Map map = new HashMap();
        try {
            Chance chance = chanceService.getChance(chcId);
            if ("succeed".equals(chcStatus)){
                chance.setChcStatus("开发成功，已归档");
            }else {
                chance.setChcStatus("开发失败，已归档");
            }
            this.chanceService.save(chance);
            map.put("result", "true");
        }catch (Exception e){
            e.printStackTrace();
            map.put("result", "false");
        }

        return map;
    }

}
